package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * @author twj
 * @description TODO
 * @time 2023/8/13^04:09:18
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler successHandler;
    @Autowired
    private AuthenticationFailureHandler failureHandler;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置认证成功/失败处理器
        http.formLogin()
                .successHandler(successHandler)
                .failureHandler(failureHandler);
        //配置注销成功处理器
        http.logout()
                .logoutSuccessHandler(logoutSuccessHandler);
        //配置任何请求都应该经过身份验证
        http.authorizeRequests().anyRequest().authenticated();
    }
}
